module view {
    //地图入口
	
    export class MapEntranceView extends eui.Component {
        closeBtn: eui.Button
        mapGroup: eui.Group
        map_420001Btn: eui.Button
        map_420002Btn: eui.Button
        map_420003Btn: eui.Button
        map_420004Btn: eui.Button
        map_420005Btn: eui.Button
        adventureInfoView: AdventureInfoView
        public constructor() {
            super()
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/MapEntranceSkin.exml"
            this.name = 'MapEntranceView'
        }

        private onComplete(): void {
            var adventureInfoView = new AdventureInfoView(this)
            adventureInfoView.y = 710
            this.addChild(adventureInfoView)
            this.adventureInfoView = adventureInfoView
            
            //this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.map_420001Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.selectMap,this)
            this.map_420002Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.selectMap,this)
            this.map_420003Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.selectMap,this)
            this.map_420004Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.selectMap,this)
            this.map_420005Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.selectMap,this)   
            //this.loadMapInfo()
        }

        private close(): void {
            this.parent.removeChild(this)
        }

        private loadMapInfo(): void {
            var mapsConfig = totalConfig[440000]
            for(var i in mapsConfig.map) {
                var mapBtn = new eui.Button()
                var mapId = mapsConfig.map[i]
                mapBtn.label = templateName[mapId]
                this.mapGroup.addChild(mapBtn)
                mapBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.selectMap,this)
                mapBtn['bigMapId'] = mapId
            }
        }

     /*   private selectMap(evt: egret.TouchEvent): void {
            var target = evt.target
            var mapId = target['bigMapId']
            var littleMapLayer = new LittleMapLayer(mapId)
            this.addChild(littleMapLayer)
        }*/
        
        private selectMap(evt: egret.TouchEvent): void {
            var target = evt.target
            var mapId = target.label
            var mapConfig = totalConfig[mapId]
            var layer 
            if(mapConfig.mapType === 2){
                layer = new LittleMapLayer(mapId)
            } else if(mapConfig.mapType === 1){
                layer = new Layer1(mapId)
            }
            
            this.addChild(layer)
        }
        
    }
    //小地图
    class LittleMapLayer extends eui.Component {
        cancelBtn: eui.Button
        confirmBtn: eui.Button
        littleMapGroup: eui.Button
        bigMapId: number
        selectLayerIndex: number
        public constructor(bigMapId) {
            super()
            this.bigMapId = bigMapId
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/MapLayerSkin.exml"
        }

        private onComplete(): void {
            this.cancelBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.entranceMap,this)
            this.showLittleMap()

        }

        private close(): void {
            console.log(this.parent)
            this.parent.removeChild(this)
        }

        private showLittleMap(): void {
            var mapConfig = totalConfig[this.bigMapId]
            var flag = true
            for(var i in mapConfig.mapLayer) {
                var selectButton: eui.RadioButton = new eui.RadioButton()
                selectButton.skinName = 'resource/control/MapSelectBtnSkin.exml'
                selectButton.label = '第' + (parseInt(i) + 1) + '层'
                selectButton.groupName = "littleMapGroup"
                selectButton.value = i
                this.littleMapGroup.addChild(selectButton)
                selectButton.addEventListener(egret.TouchEvent.TOUCH_TAP,this.selectLittleMap,this)
                if(flag) {
                    selectButton.selected = true
                    flag = false
                    this.selectLayerIndex = i
                }
            }
        }

        private selectLittleMap(evt: egret.TouchEvent): void {
            var target = evt.target
            this.selectLayerIndex = target.value
        }

        private entranceMap(evt: egret.TouchEvent): void {
            var child = new view.MapView({
                mapId: this.bigMapId,
                layerIndex: this.selectLayerIndex
            })
            this.stage.addChild(child)
            this.stage.removeChildByName('MapEntranceView')
        }
    }
    //
    class Layer1 extends eui.Component {
        cancelBtn: eui.Button
        confirmBtn: eui.Button
        panelGroup: eui.Group
        bigMapId: number
        selectLayerIndex: number
        contentLabel: eui.Label
        goldLabel: eui.Label
        bgButton: eui.Button
        selectTaskId: number
        public constructor(bigMapId) {
            super()
            this.bigMapId = bigMapId
            this.skinName = "resource/control/CityPanelSkin.exml"         
            var mapConfig = totalConfig[bigMapId]
            for(var i in mapConfig.npc){
                var button = new eui.Button
                button.skinName = "resource/control/Button2Skin.exml"
                button.label = mapConfig.npc[i].name
                this.panelGroup.addChild(button)
                button['value'] = mapConfig.npc[i]
                button.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touch,this)
            }
            this.bgButton.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
        }
        
        private touch(evt: egret.TouchEvent){
            var tagret = evt.target
            var config = tagret.value
            if(config.type === 2){//任务
                this.skinName = "resource/control/CityPanel2Skin.exml"
                var flag = true
                for(var i in config.task) {
                    var taskId = config.task[i]
                    var taskConfig = totalConfig[taskId]                   
                    var button = new eui.RadioButton
                    button.skinName = "resource/control/MapSelectBtnSkin.exml"
                    button.groupName = 'g2'
                    button.label = taskConfig.name
                    this.panelGroup.addChild(button)
                    button['value'] = taskId
                    button.addEventListener(egret.TouchEvent.TOUCH_TAP,this.taskBtnTouch,this)
                        if(flag === true){
                            flag = false
                            this.selectTaskId == taskId
                            var state = game.playerHandle.taskState(taskId)
                            if(state === 1) {
                                this.confirmBtn.label = '接受任务'
                                this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.acceptTask,this)
                            } else if(state === 2) {
                                this.confirmBtn.label = '正在做'
                                this.confirmBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.acceptTask,this)
                            } else if(state === 3) {
                                this.confirmBtn.label = '已经完成'
                                this.confirmBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.acceptTask,this)
                            }
                        }
                }    
               
            }else if(config.type === 1){ //市场
                this.skinName = "resource/control/CityPanel3Skin.exml"
                this.goldLabel.text = gplayer.basicInfo.gold + ''
                for(var i in config.sellItem) {
                    var itemId = config.sellItem[i]
                    var itemConfig = totalConfig[itemId]
                    var button = new eui.RadioButton                   
                    button.skinName = "resource/control/MapSelectBtnSkin.exml"
                    button.groupName = 'g1'
                    button.label = itemConfig.name
                    this.panelGroup.addChild(button)
                    button['value'] = itemConfig
                    button.addEventListener(egret.TouchEvent.TOUCH_TAP,this.itemBtnTouch,this)
                }
            }
            this.bgButton.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
        }
        
        private acceptTask(evt: egret.TouchEvent){
            var tagret = evt.target
            var taskId = this.selectTaskId
            
            var ret = game.playerHandle.acceptTask(taskId)
        }
        
        private taskBtnTouch(evt: egret.TouchEvent) {
            var tagret = evt.target
            var taskId = tagret.value
            var taskConfig = totalConfig[taskId]  
            this.contentLabel.text = taskConfig.content
            var state = game.playerHandle.taskState(taskId)
            this.selectTaskId = taskId
            if(state === 1){
                this.confirmBtn.label = '接受任务'
                this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.acceptTask,this)
            } else if(state === 2){
                this.confirmBtn.label = '正在做'
                this.confirmBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.acceptTask,this)
            } else if(state === 3) {
                this.confirmBtn.label = '已经完成'
                this.confirmBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.acceptTask,this)
            }
        }
        
        private itemBtnTouch(evt: egret.TouchEvent) {
            var tagret = evt.target
            var config = tagret.value
            this.contentLabel.text = config.name
        }
        
        private close(evt: egret.TouchEvent){
            this.parent.removeChild(this)
        }
    }
}
